package globulin;

import javazoom.jl.player.Player;
import javazoom.jl.decoder.JavaLayerException;

/** @author Sean Pavlichek
 */
public class MusicThread implements Runnable {
    // The music player
    Player mp3;
    MusicPlayer parent;
    int songIndex;

    /** Basic constructor.
     * @param input The Player to play the music from
     * @param inputParent The parent MusicPlayer. This is so the next song can
     * be played.
     * @param index The index of the song currently being played. This is so
     * the program can avoid playing the same song twice in a row.
     */
    public MusicThread(Player input, MusicPlayer inputParent, int index) {
        this.mp3 = input;
        this.parent = inputParent;
        this.songIndex = index;
    }

    /** The run method for this class
     */
    public void run() {
        // Plays the mp3
        try {
            mp3.play();
            while (!mp3.isComplete()) {
            }
        } catch (JavaLayerException jle) {
        } finally {
            // Closes the stream
            mp3.close();
            // Starts the next song
            parent.selectRandomSong(songIndex);
        }
    }
}